﻿namespace PetaPoco
{
    /// <summary>
    /// SQLite 数据库实现类
    /// </summary>
    internal class SQLiteBatchProvider : BatchProvider
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public SQLiteBatchProvider(IDatabase db) : base(db)
        {
            BatchSize = 30;

            /*
             * 将 entities 分批插入数据库
             * 每批生成 insert into TableName(...) select ... union all select ...
             * 该方法相对循环一条一条插入，速度提升 1/2 这样
             */
        }

        /// <summary>
        /// 生成插入语句方法
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="names"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        protected override string GenerateHeaderSql(string tableName, string[] names, string[] values)
        {
            return $"INSERT INTO {tableName} ({string.Join(",", names)}) select {string.Join(",", values)}";
        }

        /// <summary>
        /// 生成选择语句方法
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        protected override string GenerateBodySql(string[] values)
        {
            return $" union all select {string.Join(",", values)}";
        }
    }
}
